cmake_minimum_required(VERSION 3.22)
include(${CMAKE_SOURCE_DIR}/cmake/common.cmake)
# Enable CMake support for ASM and C languages
enable_language(C ASM)
# STM32CubeMX generated symbols (macros)
set(MX_Defines_Syms
        USE_HAL_DRIVER
        STM32F407xx
        $<$<CONFIG:Debug>:DEBUG>
)

# 设置顶层目录
set(SYSCALL_DIR ${CMAKE_SOURCE_DIR}/Firmware/syscall)
set(STARTUP_DIR ${CMAKE_SOURCE_DIR}/Firmware/startup)
set(DRIVERS_DIR ${CMAKE_SOURCE_DIR}/Drivers)
set(CMSIS_DIR ${DRIVERS_DIR}/CMSIS)
set(DRIVERS_SRC_DIR ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src)

# STM32CubeMX generated include paths
set(DRIVERS_INC_DIRS
        ${CMAKE_SOURCE_DIR}/Drivers
        ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Inc
        ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Inc/Legacy
        ${CMAKE_SOURCE_DIR}/Drivers/CMSIS/Device/ST/STM32F4xx/Include
        ${CMAKE_SOURCE_DIR}/Drivers/CMSIS/Include
)


# STM32 HAL/LL Drivers
file(GLOB_RECURSE STM32_Drivers_Src
        "${DRIVERS_SRC_DIR}/*.c"
        "${SYSCALL_DIR}/*.c"
        "${STARTUP_DIR}/startup_stm32f407xx.s"
)
# 排除包含 "template" 的文件
list(FILTER STM32_Drivers_Src EXCLUDE REGEX ".*template.*")

# 创建STM32驱动库的对象库 本来打算再创建几个函数用于更加细致的调控
add_library(STM32_DRIVERS OBJECT)
target_sources(STM32_DRIVERS PRIVATE ${STM32_Drivers_Src})
target_include_directories(STM32_DRIVERS PUBLIC ${DRIVERS_INC_DIRS})
target_compile_definitions(STM32_DRIVERS PRIVATE ${MX_Defines_Syms})


# Validate that STM32CubeMX code is compatible with C standard
if ((CMAKE_C_STANDARD EQUAL 90) OR (CMAKE_C_STANDARD EQUAL 99))
    message(ERROR "Generated code requires C11 or higher")
endif ()
